package cn.iocoder.yudao.module.demo.service.fileinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.demo.controller.admin.fileinfo.vo.DemoFileInfoPageReqVO;
import cn.iocoder.yudao.module.demo.controller.admin.fileinfo.vo.DemoFileInfoSaveReqVO;
import cn.iocoder.yudao.module.demo.dal.dataobject.fileinfo.DemoFileInfoDO;
import cn.iocoder.yudao.module.demo.dal.mysql.fileinfo.DemoFileInfoMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.demo.enums.ErrorCodeConstants.*;

/**
 * demo文件信息 Service 实现类
 *
 * @author li_hai
 */
@Service
@Validated
public class DemoFileInfoServiceImpl implements DemoFileInfoService {

    @Resource
    private DemoFileInfoMapper fileInfoMapper;

    @Override
    public Long createFileInfo(DemoFileInfoSaveReqVO createReqVO) {
        // 插入
        DemoFileInfoDO fileInfo = BeanUtils.toBean(createReqVO, DemoFileInfoDO.class);
        fileInfoMapper.insert(fileInfo);
        // 返回
        return fileInfo.getId();
    }

    @Override
    public void updateFileInfo(DemoFileInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateFileInfoExists(updateReqVO.getId());
        // 更新
        DemoFileInfoDO updateObj = BeanUtils.toBean(updateReqVO, DemoFileInfoDO.class);
        fileInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteFileInfo(Long id) {
        // 校验存在
        validateFileInfoExists(id);
        // 删除
        fileInfoMapper.deleteById(id);
    }

    private void validateFileInfoExists(Long id) {
        if (fileInfoMapper.selectById(id) == null) {
            throw exception(FILE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public DemoFileInfoDO getFileInfo(Long id) {
        return fileInfoMapper.selectById(id);
    }

    @Override
    public PageResult<DemoFileInfoDO> getFileInfoPage(DemoFileInfoPageReqVO pageReqVO) {
        return fileInfoMapper.selectPage(pageReqVO);
    }

}
